/**
 * 
 */
package observers.subject.impl;

import java.util.ArrayList;
import java.util.List;

import observers.observers.Observer;
import observers.subject.Subject;

/**
 * @author hoannguyen
 * 
 */
public class WeatherData implements Subject {

	private List<Observer> observers;
	private String data;

	public WeatherData() {
		observers = new ArrayList<Observer>();
		data = "";
	}

	public void dataHasChanged() {
		notifyObservers();
	}

	public void setDataValue(String data){
		this.data = data;
		
		dataHasChanged();
	}
	
	@Override
	public void registerObserver(Observer observer) {
		observers.add(observer);
	}

	@Override
	public void unregisterObserver(Observer observer) {
		int index = observers.indexOf(observer);
		if (index >= 0) {
			observers.remove(index);
		}
	}

	@Override
	public void notifyObservers() {
		for (Observer observer : observers) {
			observer.update(data);
		}
	}
}
